package com.xiaotao.service.impl;

import com.xiaotao.entity.User;
import com.xiaotao.mapper.UserMapper;
import com.xiaotao.service.UserService;
import com.xiaotao.util.JwtUtil;
import com.xiaotao.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Result login(User user) throws Exception {
        if (user.getUsername().isEmpty()||user.getPassword().isEmpty()){
            return Result.get(500,"用户名或密码不能为空");
        }
        User user1 = userMapper.findByName(user.getUsername());
        if (user1==null||!user.getPassword().equals(user1.getPassword())){
            return Result.get(500,"用户名或者密码错误");
        }
        user1.setPassword(null);
        Map<String, Object> map = new HashMap<>();
        map.put("userId",user1.getId());
        String token = JwtUtil.getToken(map);
        return Result.get(200,"登录成功",token);

    }
}
